package net.towee.server.authentication;


import javax.servlet.http.HttpSession;

import net.towee.model.account.AccountData;

/**
 * This interface define some logic for loginza authentication. It may be the
 * servlet, that gets loginza redirect, check loginza token and authenticate
 * user session.
 * 
 * @author c58
 */
public interface LoginzaAuth {
	public static final String LOGINZA_AUTH_URL = "/auth/loginza";
	
	/**
	 * Get information about login user from Loginza server by given token and
	 * widget id(wid)
	 * 
	 * @param token
	 * @return instance of LoginzaUser or null, if Loginza server response error
	 */
	LoginzaResponse getLoginzaUserByToken(String token);
	
	/**
	 * Parse JSON and return POJO representation of JSON data
	 * @param json
	 * @return
	 */
	LoginzaResponse getLoginzaUserByJSON(String json);

	/**
	 * Authenticate user by loginza response. If session already authenticated
	 * this method invoke attachLoginzaAccount and receive genereted JS with
	 * data. If account with this loginza user not exists, it creates new one
	 * and send an emailwith information about new account. If loginza user
	 * didn't contain email it do nothing(don't send email and don't put any
	 * email to new account data)
	 * 
	 * Finally it authenticate current session by calling authenticate method in
	 * SessionManager.
	 * 
	 * @param acc
	 * @param session TODO
	 * @param out TODO
	 * @return TODO
	 * @return
	 */
	boolean authenticateUser(LoginzaResponse acc, HttpSession session, Appendable out);

	/**
	 * Generate JS that set information about new attached account in parent
	 * frame and close popup with login form;
	 * 
	 * @param resp
	 * @return
	 */
	void sendJSResponse(LoginzaResponse resp, Appendable out);

	/**
	 * Create new account by LoginzaUser information
	 * 
	 * @param user
	 * @param password TODO
	 * @return
	 */
	AccountData createNewAccount(LoginzaResponse user, String password);

	/**
	 * Send email with information about new registred account
	 * 
	 * @param user
	 * @param password
	 * @return true if sending successful
	 */
	boolean sendInformationEmail(LoginzaResponse user, String password);
}
